﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Media;

namespace DynamicGeometry
{
    [Category(BehaviorCategories.Transformations)]
    [Order(3)]
    public class TranslationCreator : FigureCreator
    {
        [PropertyGridName("Translation Values")]
        public class TranslationDialog
        {
            public TranslationDialog(TranslationCreator parent)
            {
                this.parent = parent;
            }

            TranslationCreator parent;

            [PropertyGridVisible]
            [PropertyGridFocus]
            [PropertyGridName("Magnitude = ")]
            public double magnitude { get; set; }

            [PropertyGridVisible]
            [PropertyGridName("Direction = ")]
            public double direction { get; set; }

            [PropertyGridVisible]
            [PropertyGridName("Go")]
            public void Go()
            {
                if (parent.FoundDependencies.Count > 0)
                {
                    parent.AddFiguresAndRestart();
                }
            }
        }

        TranslationDialog dialog;

        public override object PropertyBag
        {
            get
            {
                if (dialog == null)
                {
                    dialog = new TranslationDialog(this);
                }
                return dialog;
            }
        }

        protected override DependencyList InitExpectedDependencies()
        {
            return DependencyList.PointVector;
        }

        protected override IFigure LookForExpectedDependencyUnderCursor(Point coordinates)
        {
            var result = Drawing.Figures.HitTest(coordinates);
            if (FoundDependencies.Count == 0)
            {
                if (Transformer.CanBeTransformSource(result))
                {
                    return result;
                }
            }
            else if (FoundDependencies.Count == 1)
            {
                if (result is Vector)
                {
                    return result;
                }
            }
            return base.LookForExpectedDependencyUnderCursor(coordinates);
        }

        protected override bool ExpectingAPoint()
        {
            return false;
        }

        protected override void AddFoundDependency(IFigure figure)
        {
            if (figure != null)
            {
                FoundDependencies.Add(figure);
            }
        }

        protected override IEnumerable<IFigure> CreateFigures()
        {
            Check.NotNull(FoundDependencies[0]);

            var vector = (FoundDependencies.Count == 2) ? FoundDependencies[1] : null;

            var results = Transformer.CreateTranslatedFigure(
                Drawing,
                FoundDependencies[0],
                vector,
                this.dialog.magnitude,
                this.dialog.direction);

            Check.NotNull(results);
            Check.NoNullElements(results);
            foreach (IFigure f in results)
            {
                yield return f;
            }
        }

        public override string Name
        {
            get { return "Translation"; }
        }

        public override string HintText
        {
            get
            {
                return " 1) Click on the figure to translate.\n 2) Either click on a vector or enter values and go.";
            }
        }

        public override FrameworkElement CreateIcon()
        {
            return IconBuilder.BuildIcon()
                .Polygon(
                    new SolidColorBrush(Colors.Yellow),
                    new SolidColorBrush(Colors.Black),
                    new Point(0.1, 0.9),
                    new Point(0.4, 0.9),
                    new Point(0.4, 0.6),
                    new Point(0.1, 0.6))
                .Polygon(
                    new SolidColorBrush(Color.FromArgb(255, 128, 255, 128)),
                    new SolidColorBrush(Colors.Black),
                    new Point(0.6, 0.4),
                    new Point(0.9, 0.4),
                    new Point(0.9, 0.1),
                    new Point(0.6, 0.1))
                .Canvas;
        }
    }
}